package com.lehecai.core.api.pstation;

import com.lehecai.core.api.AbstractApiResultBean;
import com.lehecai.core.api.ApiConstant;
import com.lehecai.core.type.pstation.PstationTaskType;
import com.lehecai.core.util.CharsetConstant;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

/**
 * Created by He Wang on 14-7-22.
 */
public class PstationParam extends AbstractApiResultBean {

	public static final String SET_NAME = "name";
	public static final String SET_KEY = "key";
	public static final String SET_CONTENT = "content";
	public static final String SET_TASk_TYPE = "type";

	private String name;// 用户自定义任务名称
	private String key;// 任务标识
	private String content;// 任务内容
	private PstationTaskType pstationTaskType;//任务类型



	public static String covertToString(PstationParam pstationParam) {

		Map<String, String> map = new HashMap<String, String>();
		if (pstationParam != null) {
			if (StringUtils.isNotEmpty(pstationParam.getName())) {
				map.put(SET_NAME, pstationParam.getName());
			}
			if (StringUtils.isNotEmpty(pstationParam.getKey())) {
				map.put(SET_KEY, pstationParam.getKey());
			}
			if (pstationParam.getPstationTaskType() != null && pstationParam.getPstationTaskType().getValue() != PstationTaskType.ALL.getValue()) {
				map.put(SET_TASk_TYPE, pstationParam.getPstationTaskType().getValue() + "");
			}
			if (!StringUtils.isEmpty(pstationParam.getContent())) {
				map.put(SET_CONTENT, pstationParam.getContent());
			}
		}
		StringBuffer sb = new StringBuffer();
		int i = 0;
		for (String key : map.keySet()) {
			if (i > 0) {
				sb.append("&");
			}
			//此处操作符不能encode,否则无法解析
			try {
				sb.append(URLEncoder.encode(key, CharsetConstant.CHARSET_UTF8)).append(ApiConstant.API_OP_EQUAL).append(URLEncoder.encode(map.get(key), CharsetConstant.CHARSET_UTF8));
			} catch (UnsupportedEncodingException e) {
				logger.error(e.getMessage(), e);
				return null;
			}
			i++;
		}
		return sb.toString();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public PstationTaskType getPstationTaskType() {
		return pstationTaskType;
	}

	public void setPstationTaskType(PstationTaskType pstationTaskType) {
		this.pstationTaskType = pstationTaskType;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}


}
